﻿using System;
using NUnit.Framework;
using OpenQA.Selenium;
using System.Threading;
using System.Collections.ObjectModel;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class H001_LuceneSeach {
		private IWebDriver driver;
		private String forumName;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			forumName = Guid.NewGuid().ToString();

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		//[Test]
		//public void OneTopicSearch() {
		//    driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
		//    driver.EnableSynchronouslySearch();
		//    driver.DisableMultiPost();
		//    driver.DisableNewUserTrial();

		//    String title = "This is supposed to be a meaningful title";
		//    String content = "And this is supposed to be a really meaningful body of text. We need to put in some proper text, and hope that search will index it!";

		//    driver.CreateTopic("Forum 2", title, content);

		//    // Let the indexer index the new topics!
		//    Thread.Sleep(1000 * 20);

		//    driver.VisitURL("forum/search");

		//    driver.InsertText("Query", "meaningful");
		//    driver.SelectOption("Forums", "Forum 2");
		//    driver.Submit("Search");

		//    // Let's see the result!!
		//    Assert.AreEqual(1, driver.FindElements(By.ClassName("topictitle")).Count);
		//}

		[Test]
		public void TestBoostSearch1() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.EnableSynchronouslySearch();
			driver.DisableMultiPost();
			driver.DisableNewUserTrial();

			String title = "Now we're going to test how boost works!";
			String content = "In version 0.7 topics are boosted if they are announcements and topics, so we should find all 3 types, and they should be ordered in a certain way.";

			driver.CreateTopic(forumName, "Sticky", title + " sticky", content);
			driver.CreateTopic(forumName, title + " regular", content);
			driver.CreateTopic(forumName, "Announcement", title + " announcement", content);
			// Let's create an extra in another forum!
			driver.CreateTopic("Forum 2", "Announcement", title + " announcement", content);

			// Let the indexer index the new topics!
			driver.Wait(20);

			driver.VisitURL("forum/search");

			IWebElement form = driver.FindElement(By.Name("MainSearchForum"));
			driver.InsertText(form.FindElement(By.Name("Query")), "boost");
			//driver.InsertText("Query", "boost");
			driver.SelectOption("Forums", forumName);
			//driver.FindElement(By.XPath("//input[@type='submit' and @name='submit']")).Click();
			driver.FindElement(By.Name("submit")).Click();
			driver.Wait(2);
			//driver.Submit("Search");

			ReadOnlyCollection<IWebElement> results = driver.FindElements(By.ClassName("topictitle"));
			Assert.AreEqual(3, results.Count);

			Assert.AreEqual(true, results[0].Text.Contains(" announcement"));
			Assert.AreEqual(true, results[1].Text.Contains(" sticky"));
			Assert.AreEqual(true, results[2].Text.Contains(" regular"));
		}

		[Test]
		public void TestBoostSearch2() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.EnableSynchronouslySearch();
			driver.DisableMultiPost();
			driver.DisableNewUserTrial();

			String title = "Nothing here that matter, the magic word is in the body!";
			String content = "We're going to a search after a word that is in the title of one topic and in the body of another, title is boosted, so we should get the result with the word in the title first in the result list! And again the magic word is lovely.";

			driver.CreateTopic(forumName, title, content);

			title = "Let's see what boosting does to title/body! The magic word is lovely";
			content = "We're going to a search after a word that is in the title of one topic and in the body of another, title is boosted, so we should get the result with the word in the title first in the result list!";

			driver.CreateTopic(forumName, title, content);

			// Let the indexer index the new topics!
			driver.Wait(20);

			driver.VisitURL("forum/search");

			driver.InsertText("Query", "lovely");
			driver.SelectOption("Forums", forumName);
			driver.Submit("Search");

			ReadOnlyCollection<IWebElement> results = driver.FindElements(By.ClassName("topictitle"));
			Assert.AreEqual(2, results.Count);

			Assert.AreEqual(true, results[0].Text.Contains(" lovely"));
			Assert.AreEqual(true, results[1].Text.Contains("Nothing"));
		}
	}
}